<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.date" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="请输入生产日期"/>
      <el-input v-model="listQuery.batchNumber" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="产品批号"/>
    <!--  <el-input v-model="listQuery.userDepartments" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="请输入使用部门"/>-->
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :loading="downloadLoading" size="mini" class="filter-item" type="warning" icon="el-icon-download" @click="handleDownload">导出</el-button>
      <el-upload class="upload-demo" action="http://139.159.147.237:8005/act/insert" :on-success="handleExcelSuccess" :on-remove="handleRemove" :before-remove="beforeRemove" :limit="1" :file-list="fileList" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, .xlsx">
        <el-button type="primary">
          选择文件
          <i class="el-icon-upload el-icon--right"></i>
        </el-button>
      </el-upload>
      <!--<el-table v-if="list.length" :data="list" style="width: 100%">
        <el-table-column prop="num" label="序号" width="100" />
        <el-table-column prop="names" label="名称" width="200" />
        <el-table-column prop="links" label="链接" width="200" />
        <el-table-column prop="passwords" label="密码" />
      </el-table>
      <el-pagination class="page" v-if="list.length" layout="prev, pager, next" :total="20" />-->
      <!--<el-upload
        style="display: inline"
        :show-file-list="false"
        :on-success="onSuccess"
        :on-error="onError"
        :before-upload="beforeUpload"
        action="/demo/admin/productProduction/insertMany">
        <el-button size="mini" type="success"  :icon="uploadBtnIcon">导入</el-button>
      </el-upload>-->
      <!--<input type="file" ref="myfile">-->
      <!--<el-button @click="importData" type="success" size="mini" icon="el-icon-upload2">导入数据</el-button>-->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="生产厂家">
              <span>{{ props.row.draftsTime }}</span>
            </el-form-item>
            <el-form-item label="出厂编号">
              <span>{{ props.row.reviewTime }}</span>
            </el-form-item>
            <el-form-item label="型号规格">
              <span>{{ props.row.certifyTime }}</span>
            </el-form-item>
            <el-form-item label="测量范围/精度">
              <span>{{ props.row.certifyTime }}</span>
            </el-form-item>
            <el-form-item label="器材价格">
              <span>{{ props.row.certifyTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="70" label="序号" prop="id" sortable/>

      <!--<el-table-column align="center" min-width="90" label="产品批号" prop="date" />-->
      <el-table-column align="center" min-width="90" label="产品批号" prop="batchNumber" />
      <el-table-column align="center" min-width="100" label="原料批号1" prop="rawNumber1" />
      <el-table-column align="center" min-width="100" label="原料批号2" prop="rawNumber2" />
      <el-table-column align="center" min-width="100" label="核素浓度" prop="rawConcentration" />
      <!--<el-table-column align="center" min-width="200" label="检定单位" prop="verfier" />-->
      <!--<el-table-column align="center" min-width="200" label="证书编号" prop="vertificateNo" />-->
      <el-table-column align="center" min-width="100" label="生产日期" prop="productionDate" />
      <!--<el-table-column align="center" min-width="200" label="是否有效" prop="isEffective" />-->
      <el-table-column align="center" min-width="80" label="口服液浓度" prop="oralConcentration" sortable/>
      <el-table-column align="center" min-width="140" label="测量时间" prop="measureTine" />
      <el-table-column align="center" min-width="90" label="取样人" prop="sampler" />
      <el-table-column align="center" min-width="140" label="核素取样时间" prop="rawSampleTime" />
      <el-table-column align="center" min-width="140" label="产品取样时间" prop="oralSampleTime" />
      <el-table-column align="center" min-width="80" label="生产量" prop="productActivity" />
      <el-table-column align="center" min-width="80" label="生产体积" prop="productCapacity" />
      <el-table-column align="center" min-width="90" label="剩余量" prop="remainingActivity" />
      <el-table-column align="center" min-width="90" label="剩余体积" prop="remainingCapacity" />
      <!--<el-table-column align="center" label="详细信息" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>-->
      <!--<el-table-column align="center" min-width="100px" label="文档类型" prop="productProductionType">
        <template slot-scope="scope">{{ scope.row.productProductionType | formatproductProductionType }}</template>
      </el-table-column>
      <el-table-column align="center" label="是否有效" prop="isEffective">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isEffective ? 'success' : 'error' ">{{ scope.row.isEffective ? '无效' : '有效' }}</el-tag>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate2(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

  </div>
</template>

<style>
	.el-dialog {
    width: 60%;
	}
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .gallery {
    width: 80px;
    margin-right: 10px;
  }
</style>

<script>

import { listproductProductions, deleteproductProductions,  insertAllproductProduction,publishproductProduction } from '@/api/business/productProduction'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import axios from 'axios'
export const uploadFileRequest=(url, params)=>{
  return axios({
    method: 'post',
    url: '/productProduction/insertMany',
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}
export default {
  name: 'productProductionList',
  components: { BackToTop, Pagination },
  filters: {
    formatproductProductionType(productProductionType) {
      if (productProductionType === 1) {
        return '操作规程'
      } else if (productProductionType === 2) {
        return '管理器材'
      } else {
        return '规章制度'
      }
    },
    formatPerson(personId) {
      if (personId === 0) {
        return '正常'
      } else if (personId === 1) {
        return '已过期'
      } else {
        return '已下架'
      }
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      loading:false,
      date: new Date(),
      files:[],
      newAttachmentsTable:[],
      insert:true,
      noPersonalInformation:[],
      listQuery: {
        page: 1,
        limit: 12,
        date: undefined,
        batchNumber: '',
        sort: 'date',
        order: 'desc'
      },
      productProductionDetail: '',
      detailDialogVisible: false,
      downloadLoading: false,
      btnText:''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    importData() {
      let myfile = this.$refs.myfile;
      let files = myfile.files;
      let file = files[0];
      var formData = new FormData();
      formData.append("file", file);
      this.uploadFileRequest("/admin/demo/productProduction/insertMany",formData).then(resp=>{
        if (resp) {
          console.log(resp);
        }
      })
    },
    getList() {
      this.listLoading = true
      listproductProductions(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    onSuccess(response, file, fileList) {
      this.enabledUploadBtn = true;
      this.files=fileList;
      this.uploadBtnIcon = 'el-icon-upload2';
      this.btnText = '数据导入';
      insertAllproductProduction(data).then(response => {
        this.$notify.success({
          title: '成功',
          message: '创建成功'
        })
        this.$router.push({ path: '/productProduction/list' })
      }).catch(response => {
        MessageBox.alert('业务错误：' + response.data.errmsg, '警告', {
          confirmButtonText: '确定',
          type: 'error'
        })
      })
    },
    importData() {
      let myfile = this.$refs.myfile;
      let files = myfile.files;
      let file = files[0];
      const formData = new FormData()
      formData.append("file", file);
      this.uploadFileRequest("/productProduction/insertMany",formData).then(resp=>{
        if (resp) {
          console.log(resp);
        }
      })
    },
 /*   onError(err, file, fileList) {
      this.enabledUploadBtn = true;
      this.uplo<el-table-column align="center" min-width="200" label="剩余量" prop="remainQuantity" />adBtnIcon = 'el-icon-upload2';
      this.btnText = '数据导入';
    },*/
    beforeUpload(file) {
      this.enabledUploadBtn = false;
      this.uploadBtnIcon = 'el-icon-loading';
      this.btnText = '正在导入';
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/productProduction/create' })
    },
    handleUpdate2(row) {
      this.$router.push({ path: '/productProduction/edit', query: { id: row.id }})
    },
    handleUpdate(row) {
      this.$router.push({ path: '/productProduction/edit', query: { id: row.id }})
    },
    showDetail(detail) {
      this.productProductionDetail = detail
      this.detailDialogVisible = true
    },

    handleDelete(row) {
      deleteproductProductions(row).then(response => {
        this.$notify.success({
          title: '成功',
          message: '删除成功'
        })
        const index = this.list.indexOf(row)
        this.list.splice(index, 1)
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      })
    },
    handleDownload() {
      this.downloadLoading = true

      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['序号', '产品批号', '原料批号1', '原料批号2', '核素浓度', '生产日期', '测量时间', '口服液浓度', '核素取样时间', '产品取样时间', '取样人', '产品生产体积', '产品活度', '剩余活度','剩余体积']
        const filterVal = ['id', 'batchNumber', 'rawNumber1', 'rawNumber2', 'rawConcentration', 'productionDate', 'measureTine', 'oralConcentration', 'rawSampleTime', 'oralSampleTime', 'measureTime', 'sampler', 'productCapacity', 'productActivity','productActivity','productActivity']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '碘131化钠口服溶液生产信息表')
        this.downloadLoading = false
      })
    }
  }
}
</script>
